package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.Favorite;
import cn.signalpha.modules.shop.mapper.FavoriteMapper;
import cn.signalpha.modules.shop.vo.FavoriteVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class FavoriteService extends ServiceImpl<FavoriteMapper, Favorite> {

	/**
	 * 自定义分页
	 */
    public IPage<FavoriteVO> selectFavoritePage(IPage<FavoriteVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectFavoritePage(page, params));
    }

    /**
	 * 自定义Wrappers
	 */
    public QueryWrapper<Favorite> selectFavoriteWrapper(Map<String, Object> params) {
        QueryWrapper<Favorite> wrapper = Wrappers.query();
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("id")), Favorite::getId, params.get("id"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("userId")), Favorite::getUserId, params.get("userId"));
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(params.get("goodsId")), Favorite::getGoodsId, params.get("goodsId"));
        wrapper.lambda().orderByDesc(Favorite::getId);
        return wrapper;
    }

	/**
	 * 获取收藏id
	 */
	public Long getUserFavoriteId(Long userId, Serializable goodsId) {
		Favorite favorite = getOne(Wrappers.<Favorite>lambdaQuery()
			.eq(Favorite::getUserId, userId).eq(Favorite::getGoodsId, goodsId));
		return ObjectUtil.isNotEmpty(favorite) ? favorite.getId() : 0L;
	}
}

